﻿/*
MAIN


*/

#include "coreDefines.h"
#include "shuffle.h"

#include "cardStack.h"
#include "turn.h"

int main(void)
{
    int cardStack[NR_CARDS];
    int playerStacks[NR_PLAYERS][NR_CARDS];
    int playedStack[NR_CARDS];
    int r = 0;
    int turns = 0;
    int executedCard = NO_CARD;
    int colorWish = NO_COLORWISH;
    //initialize decks
    initStack(&cardStack);
    initStack(&playedStack);
    for(int i = 0; i<NR_PLAYERS; i++)
    {
        initStack(&playerStacks[i]);
    }
    
    //create deck
    for(int i=0; i<NR_CARDS; i++)
    {
        
        cardStack[i] = i;
        
    }
    
    
    
    //shuffle deck
    shuffleArray(&cardStack);
    
    
    //give cards to players
    for(int i=0; i < CARDS_HAND*NR_PLAYERS; i++)
    {
        playerStacks[i%NR_PLAYERS][i/NR_PLAYERS] = removeTopCardFromStack(&cardStack);
        ;
    }
	sortPlayersHand(&playerStacks[0]);
    
    
    
    printf("---------------------------------------------------------------\n\n");

    printf("          /\\       __  __        /--\\           /\\     \n");
    printf("         /  \\     /  \\/  \\     __\\  /__        /  \\  \n");
    printf("        /    \\    \\      /    /        \\      /    \\  \n");
    printf("        \\    /     \\    /     \\_--  --_/     /      \\ \n");
    printf("         \\  /       \\  /          ##        (__/||\\__) \n");
    printf("          \\/         \\/           ##            ||      \n\n");

    printf("--------------------------- MAU MAU ---------------------------\n\n\n");
    
    //open first card
    addCardToStack(&playedStack, removeTopCardFromStack(&cardStack));
    //openCard = playedStack[0];
    //openCard = removeTopCardFromStack(&cardStack);
    
    while(r != EXIT_PROGRAM)
    {
    if(!stackContainsEnoughCardsForNextTurn(&cardStack))
    {
        recyclePlayedCards(&cardStack, &playedStack);
        printf(     "---------------------------------------------------------------\n");
        printf(     ">>> The played cards have been shuffled and added to the stack.\n");
        printf(     "---------------------------------------------------------------\n\n");

    }
    if(turns%NR_PLAYERS ==0)
    {
        printf(     ">------------------------- Your Turn -------------------------<\n");
       // printf(     "Your most common color is:");
       // printColor(getDominantColorOfstack(&playerStacks[0]));
     r = turn(&playerStacks[0], &cardStack, &playedStack, &executedCard, &colorWish);
    if(r== REPEAT_TURN)
    {
        continue;
    }
    }
    else
    {
        printf(     "------------------- Computer Player %2d Turn ------------------\n", turns % NR_PLAYERS);
       r = computerPlayerTurn(&playerStacks[turns%NR_PLAYERS], &cardStack, &playedStack, &executedCard,&colorWish);
    }
       
    if(stackIsEmpty(&playerStacks[turns%NR_PLAYERS]))
        {
           if(turns%NR_PLAYERS == 0)
           {
               printf(">>> You  won the match after %d turns! <<<\n\n",turns/NR_PLAYERS);
               break;

           }
            else
            {
                printf(">>> Computer Player %2d won the match after %d turns! <<<\n\n",turns%NR_PLAYERS,turns/NR_PLAYERS);
                break;
            }
        }
    turns++;
    }
    
    
    
    
    return 0;
}